package com.jeewms.www.tms.ui.itemview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.jeewms.www.tms.R;
import com.jeewms.www.tms.bean.bean.MessageEvent;
import com.jeewms.www.tms.bean.vm.BaseVm;
import com.jeewms.www.tms.bean.vm.DemoVm;
import com.jeewms.www.tms.constance.ClassHolder;
import com.jeewms.www.tms.constance.Constance;
import com.jeewms.www.tms.util.GsonUtils;
import com.jeewms.www.tms.util.LoadingUtil;
import com.jeewms.www.tms.util.Logutil;
import com.jeewms.www.tms.util.SharedPreferencesUtil;
import com.jeewms.www.tms.util.ToastUtil;
import com.jeewms.www.tms.util.UUIDUtil;
import com.jeewms.www.tms.util.UpdataUiUtil;
import com.jeewms.www.tms.volley.HTTPUtils;
import com.jeewms.www.tms.volley.VolleyListener;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/7.
 */

public class PandianItemView {
    View mView;
    private Context mContext;
    ViewHolder holder;
    public InventoryListent listent;

    private String ptr1 = "";
    private String ptr2 = "";
    private String ptr3 = "";


    public void setListent(InventoryListent listent) {
        this.listent = listent;
    }

    public PandianItemView(Context context) {
        mView = LayoutInflater.from(context).inflate(R.layout.item_pandian, null);
        mContext = context;
        holder = new ViewHolder(mView);
    }

    public View getmView() {
        return mView;
    }

    public void bind(final DemoVm vm, final int position, String className, final String type) {
        if (className.equals(ClassHolder.JIT)) {
            UpdataUiUtil.getDefaul().setEnadle(holder.etX1, holder.etX2, holder.etX3, holder.etX4)
                    .setText("物料号：", holder.tvX1).setText("物料描述：", holder.tvX2).setText("源库位：", holder.tvX3).setText("源储位：", holder.tvX4)
                    .setText("数量：", holder.tvX5).setText("目标库位：", holder.tvX6).setText("目标储位：", holder.tvX7);
        } else {
            UpdataUiUtil.getDefaul().setEnadle(holder.etX1, holder.etX2, holder.etX3, holder.etX4, holder.etX6).hideViews(holder.llHold7)
                    .setText("物料号：", holder.tvX1).setText("物料描述：", holder.tvX2).setText("储位：", holder.tvX3).setText("存数量：", holder.tvX4)
                    .setText("实盘数量：", holder.tvX5).setText("批次：", holder.tvX6);
            holder.etX5.setHeight(50);
        }

        UpdataUiUtil.getDefaul().setText(vm.getWmX3(),holder.etX1).setText(vm.getWmX4(),holder.etX2).setText(vm.getWmX5(),holder.etX3)
                .setText(vm.getWmX6(),holder.etX4).setText(vm.getWmX7(),holder.etX5).setText(vm.getWmX8(),holder.etX6)
                .setText(vm.getWmX9(),holder.etX7);

        holder.etX5.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus && !ptr1.equals(holder.etX5)) {
                    ptr1 = holder.etX5.getText().toString();
                    saveVm(position, holder);
                }
            }
        });
        holder.etX6.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus && !ptr2.equals(holder.etX6)) {
                    ptr2 = holder.etX6.getText().toString();
                    saveVm(position, holder);
                }
            }
        });
        holder.etX7.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus && !ptr3.equals(holder.etX7)) {
                    ptr3 = holder.etX7.getText().toString();
                    saveVm(position, holder);
                }
            }
        });
        holder.btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save(type);
            }
        });
    }

    public void saveVm(int position, ViewHolder holder) {
        //保存操作
        listent.saveVm(position, holder.etX5.getText().toString(), holder.etX6.getText().toString(), holder.etX7.getText().toString());
    }


    public interface InventoryListent {
        public void save(int position);
        public void saveVm(int position, String x5, String x6,String x7);
    }
    private void save(String type) {
        Map<String, String> map = new HashMap<>();
        map.put("wxs1", UUIDUtil.getUUID32());//uuid
        map.put("createBy", SharedPreferencesUtil.getInstance(mContext).getKeyValue(Constance.SHAREP.LOGINNAME));
//        map.put("wxs2", vm.getTinId());//类型
        map.put("wxs3", holder.etX1.getText().toString());//按顺序排
        map.put("wxs4", holder.etX2.getText().toString());//按顺序排
        map.put("wxs5", holder.etX3.getText().toString());//按顺序排
        map.put("wxs6", holder.etX4.getText().toString());//按顺序排
        map.put("wxs7", holder.etX5.getText().toString());//按顺序排
        map.put("wxs8", holder.etX6.getText().toString());//按顺序排
        map.put("wxs9", holder.etX7.getText().toString());//按顺序排
        JSONObject jsonObject = new JSONObject(map);
        String str = jsonObject.toString();
        String url = Constance.getwmSttInGoodsControllercURL() + "?doctype=" + type + "&tSapLtttstr=" + str;
        Logutil.print("url===" + url);
        HTTPUtils.get(mContext, url, new VolleyListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                LoadingUtil.hideLoading();
                ToastUtil.show(mContext, "未知错误");
            }

            @Override
            public void onResponse(String response) {
                BaseVm vm = GsonUtils.parseJSON(response, BaseVm.class);
                if (vm.isOk()) {
                    ToastUtil.show(mContext, "保存成功");
                    EventBus.getDefault().post(new MessageEvent("key6"));
                } else {
                    ToastUtil.show(mContext, "保存失败");
                }
            }
        });
    }


    static class ViewHolder {
        @BindView(R.id.tv_x1)
        TextView tvX1;
        @BindView(R.id.et_x1)
        EditText etX1;
        @BindView(R.id.tv_x2)
        TextView tvX2;
        @BindView(R.id.et_x2)
        EditText etX2;
        @BindView(R.id.tv_x3)
        TextView tvX3;
        @BindView(R.id.et_x3)
        EditText etX3;
        @BindView(R.id.tv_x4)
        TextView tvX4;
        @BindView(R.id.et_x4)
        EditText etX4;
        @BindView(R.id.tv_x5)
        TextView tvX5;
        @BindView(R.id.et_x5)
        EditText etX5;
        @BindView(R.id.tv_x6)
        TextView tvX6;
        @BindView(R.id.et_x6)
        EditText etX6;
        @BindView(R.id.tv_x7)
        TextView tvX7;
        @BindView(R.id.et_x7)
        EditText etX7;
        @BindView(R.id.ll_hold7)
        LinearLayout llHold7;
        @BindView(R.id.btn_save)
        Button btnSave;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
